package com.kayt.cyberBar.cuser.business.dao.impl;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.cuser.business.dao.ISheetUserFutureWorkDao;
import com.kayt.cyberBar.cuser.business.entity.po.BSheetUserFutureWork;

@Repository("sheetUserFutureWorkDao")
public class SheetUserFutureWorkDaoImpl implements ISheetUserFutureWorkDao {
	@Autowired
	private EntityDao entityDao;

	@Override
	public Integer save(BSheetUserFutureWork entity) throws SQLException {
		if (entity != null) {
			entityDao.save(entity);
			return 0;
		} else {
			return -1;
		}
	}

	@Override
	public Integer update(BSheetUserFutureWork entity) throws SQLException {

		if (entity != null) {
			entityDao.update(entity);
			return 0;
		} else {
			return -1;
		}
	}

	@Override
	public Integer delete(Integer id) {

		if (id != null) {
			BSheetUserFutureWork BSheetUserFutureWork = findById(id);
			if (BSheetUserFutureWork != null) {
				entityDao.delete(BSheetUserFutureWork);
				return 0;
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}

	@Override
	public BSheetUserFutureWork findById(Integer id) {

		if (id != null) {
			return entityDao.findById(id, BSheetUserFutureWork.class);
		} else {
			return null;
		}
	}

	@Override
	public PageData<BSheetUserFutureWork> findByPage(int pageNum, int pageSize, String hql, Object[] params,
			LinkedHashMap<String, String> orderMap) throws SQLException {
		return this.entityDao.getPagingData(BSheetUserFutureWork.class, pageNum, pageSize, hql, params, orderMap);
	}

	@Override
	public List<BSheetUserFutureWork> getFutureWork(String workCode, String startDate, String endDate)
			throws SQLException {
		String sql = "select * from b_sheet_user_future_work where WORK_CODE = '" + workCode + "' and WORK_TIME >= '"
				+ startDate + "' and WORK_TIME <= '" + endDate + "' order by work_start_time asc";
		return entityDao.getPagingDataBySql(BSheetUserFutureWork.class, -1, -1, sql, null);
	}

	@Override
	public BSheetUserFutureWork findBSheetUserFutureWork(String workCode, String shiftCode, String date)
			throws SQLException {
		String sql = "select * from b_sheet_user_future_work where WORK_CODE = '" + workCode + "' and SHIFT_CODE = '"
				+ shiftCode + "' and WORK_TIME = '" + date + "'";
		List<BSheetUserFutureWork> futureWorkList = entityDao.getPagingDataBySql(BSheetUserFutureWork.class, -1, -1,
				sql, null);
		if (futureWorkList != null && futureWorkList.size() != 0) {
			return futureWorkList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public BSheetUserFutureWork findBSheetUserFutureWorkByPositionCode(String positionCode, String shiftCode,
			String date) throws SQLException {
		String sql = "select * from b_sheet_user_future_work where POSITION_CODE = '" + positionCode
				+ "' and SHIFT_CODE = '" + shiftCode + "' and WORK_TIME = '" + date + "'";
		List<BSheetUserFutureWork> futureWorkList = entityDao.getPagingDataBySql(BSheetUserFutureWork.class, -1, -1,
				sql, null);
		if (futureWorkList != null && futureWorkList.size() != 0) {
			return futureWorkList.get(0);
		} else {
			return null;
		}
	}

	@Override
	public BSheetUserFutureWork findBSheetUserFutureWorkByCode(String code) throws SQLException {
		if (StringUtils.isEmpty(code))
			return null;
		return entityDao.getObj(BSheetUserFutureWork.class, "o.futureCode ='" + code + "'", null);
	}

	@Override
	public void remove(BSheetUserFutureWork entity) {
		entityDao.delete(entity);
	}

	@Override
	public void batchDelete(List<BSheetUserFutureWork> entityList) {
		entityDao.batchDelete(entityList);
	}

	@Override
	public List<Map<String, Object>> findBySotreCode(String storeCode, String date) throws SQLException {
		String hql = "select o.workCode as workCode,o.shiftCode as shiftCode,o.positionCode as positionCode, "
				+ "o.workTime as workTime, o.workStartTime as workStartTime , "
				+ "o.workEndTime as workEndTime ,uw.userCode as userCode from BSheetUserFutureWork o,BOrganizationDepartUser du"
				+ ",BOrganizationStoreDepart sd ,BSheetUserWork uw where o.workCode = "
				+ "uw.workCode and uw.userCode = du.userCode and du.storeDepartCode = "
				+ "sd.storeDepartCode and sd.storeCode = ? and o.workTime = ?";
		return entityDao.getPagingDataByHql(-1, -1, hql, new Object[] { storeCode, date });
	}
}
